<?php
/**
居所用户管理
 */

namespace App\Http\Controllers\Property;


use App\Common\json;
use App\Common\SmsClient;
use App\Http\Controllers\Controller;
use App\Libraries\upload;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class TenementController extends  Controller{

    //获取
    public function SelectTenement(Request $request){
        if($request ->isMethod('post')){
            $myaddress_id   = $request ->input('myaddress_id');
            $data          = DB::table('tenement')
                           ->select('tenement_head','tenement_phone','tenement_name','identity')
                           ->leftjoin('myaddress','tenement.myaddress_tenement','=','myaddress.myaddress_id')
                           ->where('myaddress_tenement',$myaddress_id)
                           ->get();
            if($data == true){
                return json::json('success', '修改新住户成功', $data);
            }else{
                return json::json('failure','由于网络或者不正当原因失败',[]);
            }
        }
    }
    //添加
    public function AddTenement(Request $request){
        if($request ->isMethod('post')){
            $myaddress_id      = $request ->input('myaddress_id');
            $tenement_head     = $request ->input('tenement_head');
            $tenement_phone    = $request ->input('tenement_phone');
            $tenement_name     = $request ->input('tenement_name');
            $identity          = $request ->input('identity');
            $select            = DB::table('tenement')
                               ->where('myaddress_tenement',$myaddress_id)
                               ->where('tenement_phone',$tenement_phone)
                               ->where('tenement_name',$tenement_name)
                               ->where('identity',$identity)
                               ->get();
            if($select == true){
                return json::json('success', '添加的新住户已经存在', json::value(1));
            }else{
                $data         = DB::table('tenement')
                              ->insert([
                                  'myaddress_tenement'=>$myaddress_id,
                                  'tenement_head'=>$tenement_head,
                                  'tenement_phone'=>$tenement_phone,
                                  'tenement_name' =>$tenement_name,
                                  'identity' =>$identity
                              ]);
                if($data == true){
                    return json::json('success', '添加新住户成功', json::value(1));
                }else{
                    return json::json('failure','由于网络或者不正当原因失败',json::value(1));
                }
            }
        }
    }

    //修改
    public function UpdateTenement(Request $request){
        if($request ->isMethod('post')){
            $tenement_head     = $request ->input('tenement_head');
            $tenement_phone    = $request ->input('tenement_phone');
            $tenement_name     = $request ->input('tenement_name');
            $identity          = $request ->input('identity');
            $tenement_id       = $request ->input('tenement_id');
            $data              = DB::table('tenement')
                               ->where('tenement_id',$tenement_id)
                               ->update([
                                   'tenement_head'=>$tenement_head,
                                   'tenement_phone'=>$tenement_phone,
                                   'tenement_name'=>$tenement_name,
                                   'identity'=>$identity
                               ]);

            if($data == true){
                return json::json('success', '修改新住户成功', json::value(1));
            }else{
                return json::json('failure','由于网络或者不正当原因失败',json::value(1));
            }

        }
    }

    //删除
    public function DelTenement(Request $request){
        if($request ->isMethod('post')){
            $tenement_id       = $request ->input('tenement_id');
            $data              = DB::table('tenement')
                ->where('tenement_id',$tenement_id)
                ->delete();
            if($data == true){
                return json::json('success', '删除新住户成功', json::value(1));
            }else{
                return json::json('failure','由于网络或者不正当原因失败',json::value(1));
            }
        }
    }



}